package com.xiaorun.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaorun.portal.dao.NoticeMapper;
import com.xiaorun.portal.modal.Bo.Query;
import com.xiaorun.portal.modal.Vo.Notice;
import com.xiaorun.portal.service.INoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by BlueT on 2018/3/3.
 */
@Service
@Slf4j
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public PageInfo<Notice> list(int pageNum, int pageSize, Map<String, Object> params) {
        PageHelper.startPage(pageNum, pageSize);
        Query query = new Query(params != null ? params: new HashMap<>());
        List<Notice> noticeList = noticeMapper.list(query);

        noticeList.forEach(item ->{

            String summary = item.getContent().replaceAll("</?[^>]+>", ""); //剔出html的标签
            summary = summary.replaceAll("\\s*|\t|\r|\n", "");//去除字符串中的空格,回车,换行符,制表符
            summary = summary.replaceAll("&nbsp;", "");
            summary = summary.substring(0, 100);
            item.setSummary(summary + "...");
        });


        PageInfo<Notice> pageInfo = new PageInfo(noticeList);
        return pageInfo;
    }

    @Override
    public int save(Notice notice){
        int result;
        if(notice.getId() != null){
            result = noticeMapper.update(notice);
        }else{
            result = noticeMapper.save(notice);
        }
        return result;
    }

    @Override
    public Notice get(int id) {
        return noticeMapper.get(id);
    }

    @Override
    public int remove(int id) {
        return noticeMapper.remove(id);
    }

    @Override
    public int countTotal(Map<String, Object> params) {
        Query query = new Query(params != null ? params: new HashMap<>());
       return noticeMapper.countTotal(query);
    }



}
